<?php

/**
 * 仓库
 * @author wangwx 2015-5-7
 */
namespace common\repositories\myhtsq;

use common\entities\ConfigEntity;
use common\repositories\BaseRepository;
use common\entities\myhtsq\ContractApplicationEntity;
use common\support\StringHelper;

class ContractApplicationRepository extends BaseRepository
{

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = [])
    {
        parent::__construct($config);
    }

    public static function handleApplicationBalanceData($applications)
    {
        foreach ($applications as $application) {
            $entity = new ContractApplicationEntity();
            $entity = $entity::find()->where(['contract_application_id' => $application['contract_application_id']])->one();
            $entity->load($application, '');
            $entity->save();
        }
    }

    /**
     * 处理合同签约产品数组
     * @param int $contractId 合同id
     * @param array $productArr 签约合同数组
     * @return boolean
     */
    public static function handleApplicationData($contractId, $productArr)
    {
        if (empty($productArr)) {//没有产品，直接返回
            return true;
        }
        foreach ($productArr as $key => $val) {
            if (!isset($val['__type'])) {
                $val['__type'] = 'update';
            }
            switch ($val['__type']) {
                case 'add':

                    $myscrmContractApplicationEntity = new ContractApplicationEntity();
                    $myscrmContractApplicationEntity->load($val, '');
                    $myscrmContractApplicationEntity->contract_application_id = StringHelper::uuid();
                    $myscrmContractApplicationEntity->contract_id = $contractId;
                    $myscrmContractApplicationEntity->created_on = ConfigEntity::getDbNowDate();
                    $myscrmContractApplicationEntity->save();
                    break;
                case 'delete':
                    ContractApplicationEntity::deleteAll(['contract_id' => $contractId, 'contract_application_id' => $val['contract_application_id']]);
                    break;
                case 'update':
                case '':
                    $myscrmContractApplicationEntity = new ContractApplicationEntity();
                    $myscrmContractApplicationInfo = $myscrmContractApplicationEntity::find()->where(['contract_application_id' => $val['contract_application_id']])->one();
                    $myscrmContractApplicationInfo->load($val, '');
                    $myscrmContractApplicationInfo->save();
                    break;
            }

        }
        return true;
    }

    /**
     * 获取合同id所有签约产品
     * @param string $contractId 合同id
     * @return array
     */
    public static function getAllContractApplicationByContractId($contractId)
    {
        return ContractApplicationEntity::find()->select("*")->leftJoin("myscrm_application_info", "myscrm_application_info.application_id =myscrm_contract_application.application_id")
            ->where(['contract_id' => $contractId])
            ->andWhere(['or',"BusinessTypeName='SAAS产品'","BusinessTypeName='SAAS其他'","BusinessTypeName='SAAS专项'"])
            ->asArray()->all();
    }

    /**
     * 根据合同id,判断是否存在产品
     * @param string $contractId 合同id
     * @return boolean
     */
    public static function isExistContractApplicationByContractId($contractId)
    {
        return ContractApplicationEntity::find()->where(['contract_id' => $contractId])->exists();
    }

    /**
     * @param $contract_id
     * @param $application_id
     * @return mixed
     */
    public static function getMaxNumOfApp($contract_id,$application_id){
        return ContractApplicationEntity::find()->where(['contract_id' => $contract_id,'application_id'=>$application_id])->sum('authorization_numeric');
    }
}